/**
 *
 * @file utils.h
 *
 * @brief Various utilities
 *
 * $Id: utils.h 123 2009-01-15 08:42:49Z henri.doreau $
 */

/*
 * This file is part of Tsar.
 *
 * Tsar is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tsar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tsar.  If not, see <http://www.gnu.org/licenses/>.
 */


#ifndef UTILS_H
#define UTILS_H


/* -- S T A N D A R D  I N C L U D E S -- */
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>


/* -- O T H E R  I N C L U D E S -- */
/* FOR PRINTING FUNCTIONS : will move */
#include <stdarg.h>

/* -- C O N S T A N T S -- */

/* -- M A C R O S -- */
/* expanded macro to convert values to string */
#define str( s )    _str( s )
#define _str( s )   #s

/* DEV */
#define MOUCHARD (fprintf( stderr, "<<DEBUG>>!!!\n\tFile : %s\n\tFunction : %s\n\tLine : %d\n\terrno : %m (%d)\n", __FILE__, __FUNCTION__, __LINE__, errno ))


/* -- T Y P E D E F S -- */
/** @brief let's reinvent booleans */
typedef enum {
    false,
    true
} bool_t;


/* -- F U N C T I O N S  P R O T O T Y P E S -- */
/**
 * @brief convert a string to an unsigned long integer
 * @param str is a null terminated string containing the digits
 * @param dstNumber points to an allocated unsigned long int we'll fill
 * @return error code according to the execution of the function
 * @retval 0 conversion succeeded
 * @retval 1 overflow
 * @retval 2 not a valid number
 * @retval 3 number is followed by extra characters
 */
int str2ulong( char * str, unsigned long * dstNumber );

/**
 * @brief open a file and return the file pointer
 * @param filename is the name used to open the file 
 * @param mode mode of openning
 * @return The file pointer
 */
FILE * Fopen( char * filename, char * mode );

/**
 * @brief malloc wrapper that die on failure
 * @param size is the number of bytes to alloc
 * @return a valid pointer to the allocated area
 */
void * Malloc( size_t size );

/**
 * @brief yet another system wrapper, this one check for NULL pointers before to free them
 * @param p pointer to the heap zone to free
 */
void Free( void * p );

/**
 * @brief realloc wrapper that die on failure
 * @param ptr points to the area to resize
 * @param size is the number of bytes to alloc
 * @return a valid pointer to the allocated area
 */
void * Realloc( void * ptr, size_t size );

#endif /* UTILS_H */

